/** 注释
 * @Author FiveXu(316643470@qq.com)
 * @Description: deskApp
 * @Date  2023-10-14 12:58
 */
import { defineStore } from 'pinia'
import { randomWord } from '@/utils/utils'

export type DeskAppComponent =
  | 'folder'
  | 'weChat'
  | 'QQ'
  | 'chrome'
  | 'setting'
  | 'geek'
  | 'vsCode'
  | 'kuGou'
  | 'wps'
  | 'videoPlayer'

export interface IDestApp {
  readonly prod?: boolean // 表示这个程序是否是开发中
  readonly onlyApp: boolean // 表示这个程序只能打开一个
  readonly id: string // 程序的唯一标识
  readonly appSize: number // 程序的物理大小 单位是 b
  readonly createTime: number // 程序的创建时间 这里是程序的上市时间
  readonly appId: DeskAppComponent // 程序的类型
  component: DeskAppComponent // 程序的组件名称
  isResize: boolean // 程序当前是否可以改变大小
  name: string // 程序名
  icon: string // 程序图标
  iconIos: string // iOS程序图标
  iOSBar: string[] // iOS下顶部
  isChecked: boolean // 程序是否在桌面中选中
  tray: boolean // 程序是否在系统托盘中显示
  trayIcon: string[] // 系统托盘图标
  taskBar: boolean // 程序是否在任务栏中显示
  isTaskBar: boolean // 程序是否在任务栏中显示
  isCenter: boolean // 程序是否在中心显示
  isSystemTopBar: boolean // 程序是否存在系统拖动条
  size: {
    // 程序窗口大小 [width, height, minWidth, minHeight, maxWidth, maxHeight, 是否可以拖动大小 1-是 0-否]
    default: number[]
    [key: string]: number[]
  }
  x?: number // 程序的初始位置 X轴
  y?: number // 程序的初始位置 Y轴
  trayMenu?: string // 程序是否存在系统托盘菜单
  componentData?: {} // 组件自己的数据
}

export interface IDeskAppStore {
  deskAppList: IDestApp[]
  systemAppList: IDestApp[]
}

const DESK_APP_NAME = 'desk-app'

export const useDeskAppStore = defineStore('DeskApp', {
  state: (): IDeskAppStore => {
    return {
      deskAppList: [
        {
          prod: true,
          onlyApp: true,
          appSize: 565,
          createTime: new Date('2011-01-21').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-weChat-`),
          name: '微信',
          appId: 'weChat',
          icon: 'weChat',
          iconIos: 'weChat-ios',
          iOSBar: ['t1', 't2', 't3', 't4', 't5', 't6'],
          component: 'weChat',
          isChecked: false,
          isResize: false,
          tray: true,
          trayIcon: ['weChat'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [280, 380, 0, 0, 0, 0, 0],
            content: [1000, 618, 700, 500]
          },
          trayMenu: 'weChat'
        },
        {
          prod: true,
          onlyApp: false,
          appSize: 701,
          createTime: new Date('1999-02').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-QQ-`),
          name: '腾讯QQ',
          appId: 'QQ',
          icon: 'QQ',
          iconIos: 'QQ-ios',
          iOSBar: ['t1', 't2', 't3', 't4'],
          component: 'QQ',
          isChecked: false,
          isResize: true,
          tray: true,
          trayIcon: ['QQ-black', 'QQ'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [430, 330, 0, 0, 0, 0, 0],
            list: [300, 700, 300, 500, 600, 0, 0]
          },
          trayMenu: 'QQ'
        },
        {
          prod: true,
          onlyApp: true,
          appSize: 581,
          createTime: new Date('2004-01').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-kuGou-`),
          name: '酷狗音乐',
          appId: 'kuGou',
          icon: 'kuGou',
          iconIos: 'kuGou-ios',
          iOSBar: ['t1', 't2', 't3', 't4'],
          component: 'kuGou',
          isChecked: false,
          isResize: true,
          tray: true,
          trayIcon: ['kuGou'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [1020, 720, 1020, 720, 0, 0, 0]
          },
          trayMenu: 'kuGou'
        },
        {
          prod: true,
          onlyApp: true,
          appSize: 3581,
          createTime: new Date('2009-01').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-videoPlayer-`),
          name: '完美播放器',
          appId: 'videoPlayer',
          icon: 'videoPlayer',
          iconIos: 'videoPlayer',
          iOSBar: ['t1'],
          component: 'videoPlayer',
          isChecked: false,
          isResize: true,
          tray: false,
          trayIcon: ['videoPlayer'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [1020, 720, 1020, 720, 0, 0, 0]
          }
        },
        {
          prod: true,
          onlyApp: false,
          appSize: 2.3 * 1024,
          createTime: new Date('2008-09').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-chrome-`),
          name: 'Google\nChrome',
          appId: 'chrome',
          icon: 'chrome',
          iconIos: 'chrome-ios',
          iOSBar: ['t1', 't2', 't3', 't4', 't5', 't6', 't7', 't8', 't9', 't10'],
          component: 'chrome',
          isChecked: false,
          isResize: true,
          tray: false,
          trayIcon: ['chrome'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [1000, 618, 500, 480]
          }
        },
        {
          prod: true,
          onlyApp: true,
          appSize: 6.69 * 1024 * 1024,
          createTime: new Date('2019-09-11').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-geek-`),
          name: 'Geek 卸载',
          appId: 'geek',
          icon: 'geek',
          iconIos: 'geek',
          iOSBar: ['t1'],
          component: 'geek',
          isChecked: false,
          isResize: true,
          tray: false,
          trayIcon: ['geek'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: true,
          size: {
            default: [460, 600, 460, 600]
          }
        },
        {
          prod: true,
          onlyApp: false,
          appSize: 1.41 * 1024,
          createTime: new Date('2015-04-29').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-vsCode-`),
          name: 'Visual Studio Code',
          appId: 'vsCode',
          icon: 'vsCode',
          iconIos: 'vsCode-ios',
          iOSBar: ['t1', 't2', 't3', 't4', 't5', 't6', 't7', 't8', 't9'],
          component: 'vsCode',
          isChecked: false,
          isResize: true,
          tray: false,
          trayIcon: ['vsCode'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [1000, 618, 400, 270]
          }
        },
        {
          prod: true,
          onlyApp: false,
          appSize: 871,
          createTime: new Date('2005-09-12').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-wps-`),
          name: 'WPS Office',
          appId: 'wps',
          icon: 'wps',
          iconIos: 'wps-ios',
          iOSBar: ['t1'],
          component: 'wps',
          isChecked: false,
          isResize: true,
          tray: false,
          trayIcon: ['wps'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [1000, 618, 672, 392]
          }
        },
        {
          prod: true,
          onlyApp: false,
          appSize: 1023 * 1024,
          createTime: new Date('2005-09-12').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-wps-`),
          name: '个人简历.pdf',
          appId: 'wps',
          icon: 'pdf',
          iconIos: 'pdf',
          iOSBar: ['t1'],
          component: 'wps',
          isChecked: false,
          isResize: true,
          tray: false,
          trayIcon: ['wps'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [1000, 618, 672, 392]
          },
          componentData: {
            currentId: 'resume.pdf'
          }
        },
        {
          prod: true,
          onlyApp: false,
          appSize: 1023 * 1024,
          createTime: new Date('2005-09-12').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-wps-`),
          name: 'vue2.0.docx',
          appId: 'wps',
          icon: 'word',
          iconIos: 'word-ios',
          iOSBar: ['t1'],
          component: 'wps',
          isChecked: false,
          isResize: true,
          tray: false,
          trayIcon: ['wps'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [1000, 618, 672, 392]
          },
          componentData: {
            currentId: 'vue2.0.docx'
          }
        },
        {
          prod: true,
          onlyApp: false,
          appSize: 336 * 1024,
          createTime: new Date('2005-09-12').getTime(),
          id: randomWord(20, `${DESK_APP_NAME}-wps-`),
          name: '年度考情报表.xlsx',
          appId: 'wps',
          icon: 'excel',
          iconIos: 'excel-ios',
          iOSBar: ['t1'],
          component: 'wps',
          isChecked: false,
          isResize: true,
          tray: false,
          trayIcon: ['wps'],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [1000, 618, 672, 392]
          },
          componentData: {
            currentId: 'test.xlsx'
          }
        }
      ],
      systemAppList: [
        {
          prod: true,
          onlyApp: false,
          appSize: 0,
          createTime: 0,
          id: randomWord(20, `${DESK_APP_NAME}-folder-`),
          name: '此电脑',
          appId: 'folder',
          icon: 'folder',
          iconIos: 'folder',
          iOSBar: [],
          component: 'folder',
          isChecked: true,
          isResize: true,
          tray: false,
          trayIcon: [],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: true,
          size: {
            default: [1000, 618, 160, 240]
          }
        },
        {
          prod: true,
          onlyApp: true,
          appSize: 0,
          createTime: 0,
          id: randomWord(20, `${DESK_APP_NAME}-taskApp-`),
          name: '设置',
          appId: 'setting',
          icon: 'setting',
          iconIos: 'setting',
          iOSBar: [],
          component: 'setting',
          isChecked: true,
          isResize: true,
          tray: false,
          trayIcon: [],
          taskBar: true,
          isTaskBar: true,
          isCenter: true,
          isSystemTopBar: false,
          size: {
            default: [1000, 618, 500, 320]
          }
        }
      ]
    }
  },
  actions: {
    // 把某个值设置为相同
    resetDeskAppKey<T extends keyof IDestApp>(key: T, value: IDestApp[T]) {
      this.deskAppList.forEach((item) => {
        item[key] = value
      })
    },
    // 选择App
    checkDeskApp(id: string, closeOther: boolean = true) {
      if (closeOther) this.resetDeskAppKey('isChecked', false)
      const index = this.deskAppList.findIndex((item) => item.id === id)
      this.deskAppList[index].isChecked = true
    }
  },
  persist: true
})
